package com.team5101.controller;


import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.team5101.entity.Cart;
import com.team5101.entity.CartDTO;
import com.team5101.entity.VO.SeatInfoVO;
import com.team5101.service.ICartService;
import com.team5101.service.ISeatService;
import com.team5101.utils.DateUtil;
import com.team5101.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    @Autowired
    private ISeatService seatService;

    @RequestMapping("/qurycart/{uid}")
    public R quryUserCartDTO(@PathVariable Integer uid){
        List<CartDTO> cartDTOS = cartService.quryUserCartDTO(uid);
        return R.ok(cartDTOS);
    }


    @RequestMapping("/remove/{cid}")
    public  R deleteByCartId(@PathVariable  Integer cid){
        int i = cartService.deleteByCartId(cid);
        if(i>=1){
              return R.ok();
        }
        return R.error();
    }


    @RequestMapping("addcart")
    public R addCart(SeatInfoVO seatInfoVO){

        //InsertSeatByHallID(seatInfoVO);

        System.out.println(seatInfoVO);
        Cart cart = new Cart();
        String nums = seatInfoVO.getNums();
        //"[59,60]"
        if(nums.length()>=1){
            String substring = nums.substring(0, nums.length()-1);
            String[] split = substring.split(",");
//        for(int i=0;i<split.length;i++){
//            System.out.println(split[i]);
//        }
            //59,60  座位号
            StringBuffer buffer = new StringBuffer();
            for(int i=0;i<split.length;i++){
            //System.out.println(split[i]);
                buffer.append(split[i]);
            }
            String s = buffer.toString();
            System.out.println("s为"+s);
            cart.setcSeat(split.length+"");
            cart.setcId(DateUtil.getid()+"");
            cart.setNfId(seatInfoVO.getNf_id());
            cart.setuId(seatInfoVO.getUid());
            cart.setcState(seatInfoVO.getStateType());
            cart.setcNum(s);
            cart.setsId(seatInfoVO.getSid());

            return R.ok(cartService.addCart(cart));
        }

        return  R.error();
    }

//    public R InsertSeatByHallID(SeatInfoVO seatInfoVO){
//        /*从前端获取的 参数： 订单详情数据 */
//
//        String  data=seatInfoVO.getNums();
//
//        //后端的jackson  进行转换： 将 String ====》List<OrderCourse>
//        ObjectMapper objectMapper = new ObjectMapper();
//        //json转list<User>
//        JavaType jt = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Integer.class);
//
//
//
//        List<Integer> nums=null;
//
//
//        try {
//            nums = objectMapper.readValue(data, jt);
//            if(seatInfoVO.getNumSeat()==null)
//                seatInfoVO.setNumSeat(nums);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return R.error(10000,"订单详情信息不正确！");
//        }
//        System.out.println(seatInfoVO);
//        return R.ok(seatService.InsertSeatByHallID(seatInfoVO));
//    }
}
